{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sonic Py(thon)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.insert(0, os.path.abspath('./..'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from jupylet.audio.bundle import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "app = sonic_py()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# Set up a beautiful reverb effect that adds a sense of space and a touch of realism \n",
    "# to the synthesized instrument. Try it with a pair of good headphones. \n",
    "# 设置一个美丽的混响效果，为合成乐器增加空间感和真实感。\n",
    "# 用一副好耳机试试。\n",
    "#\n",
    "set_effects(ConvolutionReverb('./sounds/impulses/MaesHowe.flac'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hammond = Hammond()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#.precussion = True\n",
    "#hammond.reverb = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hammond.overdrive = True\n",
    "#hammond.over.amp = 0.25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#hammond.chorus = True\n",
    "#hammond.set_vibrato_and_chorus('c1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Play C5 note for the duration of a full note. \n",
    "# 在完整音符的持续时间内播放C5音符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hammond.play(C5, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Play C5 note indefinately. When you play another note it will replace the previous one.\n",
    "# 不确定地演奏C5音符。当你弹奏另一个音符时，它将取代上一个音符。\n",
    "# Finally release the note to end it.\n",
    "# 最后，释放(停止)音符结束它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hammond.play(C5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hammond.play(F5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hammond.play_release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Call play_poly() to play more than one note at once. but keep a reference to the new\n",
    "# note to release it later. \n",
    "# 调用play_poly() 以一次播放多个音符。\n",
    "# 但请注意保留对新音符的引用，以便后面释放(停止)它。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = hammond.play_poly(C5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = hammond.play_poly(F5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.play_release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.play_release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you forget to keep a reference to a note playing indefinately, you can stop all \n",
    "# sounds by calling stop() \n",
    "# 如果您忘记保留对不确定播放音符的引用，可以通过调用stop() 来停止所有声音"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hammond.play_poly(C4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "use(hammond)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "play(C5, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "play(C4, 1)\n",
    "play(F4, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "play(Ds4, 1)\n",
    "play(Gs4, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "play(C4, 1)\n",
    "play(F4, 1)\n",
    "\n",
    "await sleep(1)\n",
    "\n",
    "play(Ds4, 1)\n",
    "play(Gs4, 1)\n",
    "\n",
    "await sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@app.sonic_live_loop2\n",
    "async def loop0():\n",
    "            \n",
    "    use(hammond)\n",
    "    \n",
    "    play(C4, 3/4)\n",
    "    play(F4, 3/4)\n",
    "\n",
    "    await sleep(1) \n",
    "    \n",
    "    play(Ds4, 3/4)\n",
    "    play(Gs4, 3/4)\n",
    "\n",
    "    await sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "app.stop(loop0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# The following line turns MIDI on. If you have an electric piano, hook it up \n",
    "# to your computer and it should just work.\n",
    "# 下一行打开MIDI。\n",
    "# 如果你有一架电动钢琴，把它连接到你的电脑上，它就可以正常工作了。\n",
    "#\n",
    "app.set_midi_sound(hammond)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Uncomment the following line to reduce MIDI latency to minimum. If this change introduces\n",
    "# sound artefacts, either set latency back to 'high' or switch your computer power\n",
    "# management to performance mode.\n",
    "# 取消对以下行的注释，将MIDI延迟降至最低。\n",
    "# 如果此更改引入了声音人工干扰，请将延迟设置回'high' ，\n",
    "# 或将计算机电源管理切换到性能模式。\n",
    "#\n",
    "set_latency('lowest')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@app.sonic_live_loop2\n",
    "async def loop1():\n",
    "            \n",
    "    use(hammond, amp=0.1)\n",
    "    \n",
    "    play(C3, 1/2)\n",
    "    play(F3, 1/2)\n",
    "    await sleep(1/2)\n",
    "\n",
    "    play(C3, 1/4)\n",
    "    play(F3, 1/4)\n",
    "    await sleep(1/4)\n",
    "\n",
    "    play(C3, 1/4)\n",
    "    play(F3, 1/4)\n",
    "    await sleep(1/4)\n",
    "                \n",
    "    play(Ds3, 1/4)\n",
    "    play(Gs3, 1/4)\n",
    "    await sleep(1/4)\n",
    "                \n",
    "    play(Ds3, 1/2)\n",
    "    play(Gs3, 1/2)\n",
    "    await sleep(1/2)\n",
    "    \n",
    "    play(C3, 3/4)\n",
    "    play(F3, 3/4)\n",
    "    await sleep(3/4)\n",
    "\n",
    "    play(C3, 1/4)\n",
    "    play(F3, 1/4)\n",
    "    await sleep(1/4)\n",
    "\n",
    "    play(C3, 1/4)\n",
    "    play(F3, 1/4)\n",
    "    await sleep(1/4)\n",
    "                \n",
    "    play(Ds3, 1/4)\n",
    "    play(Gs3, 1/4)\n",
    "    await sleep(1/4)\n",
    "                \n",
    "    play(F3, 1/2)\n",
    "    play(As3, 1/2)\n",
    "    await sleep(1/2)\n",
    "    \n",
    "    play(Ds3, 1/4)\n",
    "    play(Gs3, 1/4)\n",
    "    await sleep(1/4)\n",
    "                      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "app.stop(loop1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "nbTranslate": {
   "displayLangs": [
    "en"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "cn",
   "useGoogleTranslate": true
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
